import request from "@/axios/request";

const {ElMessage} = require("element-plus");

export default {
    name: "HouseAudition",
    data() {
        return {
            search: "",
            currentPage: 1,
            pageSize: 10,
            total: 0,
            allData: [],       // 存储全量数据
            tableData: []     // 当前页数据
        };
    },
    computed: {
        paginatedData() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            return this.allData.slice(start, end);
        }
    },
    created() {
        this.load();
        this.loading = true;
        setTimeout(() => {
            this.loading = false;
        }, 1000);
    },
    methods: {
        async load() {
            try{
                const res = await request.get("/house/status?status=pending");
                if (res.code === 200) {
                    this.allData = res.data || [];
                    this.total = this.allData.length;
                    this.tableData = this.paginatedData;
                } else {
                    ElMessage.error(res.msg || "加载失败");
                    this.allData = [];
                    this.tableData = [];
                    this.total = 0;
                }
                this.loading = false;
            } catch (err){
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
                this.allData = [];
                this.tableData = [];
                this.total = 0; 
                this.loading = false;
            }
        },
        async filterData() {
            const res = await request.get("/house/status?status=pending");
            if (res.code === 200) {
                this.allData = res.data || [];
                this.total = this.allData.length;
                this.tableData = this.paginatedData;
            } 

            let data = this.allData;
            if (this.search) {
                const id = parseInt(this.search);
                data = data.filter(item => item.landlordId === id);
            } else {
                this.load();
            }
            this.allData = data;
            this.total = data.length;
            this.tableData = this.paginatedData;
        },
        reset() {
            this.search = "";
            this.currentPage = 1;
            this.load();
        },
        async handleConfirm(houseId) {
            try {
                const res = await request.put(`/house/statu?houseId=${houseId}&status=available`);
                if (res.code === 200) {
                    ElMessage.success("成功提交审核结果");
                    this.load();
                } else {
                    ElMessage.error(res.msg || "审核失败");
                }
            } catch (err) {
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
            }
        },
        async handleReject(houseId) {
            try {
                const res = await request.put(`/house/statu?houseId=${houseId}&status=rejected`);
                if (res.code === 200) {
                    ElMessage.success("成功提交审核结果");
                    this.load();
                } else {
                    ElMessage.error(res.msg || "审核失败");
                }
            } catch (err) {
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
            }
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.tableData = this.paginatedData;
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.tableData = this.paginatedData;
        },
    },
};